home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDIB.PAK / GDIDIB.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  14KB  |  474 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: gdidib.c
  9. //
  10. //  PURPOSE: Handles general routines for the GDIDIB sample.
  11. //
  12. //  FUNCTIONS:
  13. //    WndProc         - Processes messages for the main window.
  14. //    MsgCommand      - Handles the WM_COMMAND messages for the main window.
  15. //    MsgCreate       - Handles the WM_CREATE message.  Creates the toolbar,
  16. //                      statusbar, and client windows.
  17. //    MsgSize         - Handles the WM_SIZE message by passing the message
  18. //                      on to the toolbar and statusbar controls and then
  19. //                      resizing the client window between them. 
  20. //    MsgPaletteChanged - Passes WM_PALETTECHANGED message to 
  21. //                      ProcessPaletteChanged.
  22. //    MsgQueryNewPalette - Passes WM_QUERYNEWPALETTE message to
  23. //                      ProcessQueryNewPalette.
  24. //    MsgClose        - Handles the WM_CLOSE message for the main window.
  25. //    MsgDestroy      - Handles the WM_DESTROY message by calling
  26. //                      PostQuitMessage().
  27. //    CmdExit         - Handles the file exit command by calling destroy
  28. //                      window on the main window.
  29. //
  30. //  COMMENTS:
  31. //    Message dispatch table -
  32. //      For every message to be handled by the main window procedure
  33. //      place the message number and handler function pointer in
  34. //      rgmsd (the message dispatch table).  Place the prototype
  35. //      for the function in globals.h and the definition of the
  36. //      function in the appropriate module.
  37. //    Command dispatch table -
  38. //      For every command to be handled by the main window procedure
  39. //      place the command number and handler function pointer in
  40. //      rgcmd (the command dispatch table).  Place the prototype
  41. //      for the function in globals.h and the definition of the
  42. //      function in the appropriate module.
  43. //    Globals.h Contains the definitions of the structures and dispatch.c
  44. //      contains the functions that use these structures.
  45. //
  46.  
  47. #include <windows.h>            // required for all Windows applications
  48. #include <windowsx.h>
  49. #include <commctrl.h>           // prototypes and defs for common controls
  50. #include "globals.h"            // prototypes specific to this application
  51. #include "resource.h"
  52. #include "toolbar.h"            // prototypes for the tool bar
  53. #include "statbar.h"            // prototypes for the status bar
  54. #include "palette.h"            // prototypes for palette routines
  55.  
  56. // Main window message table definition.
  57. MSD rgmsd[] =
  58. {
  59.     {WM_MENUSELECT,      MsgMenuSelect},
  60.     {WM_COMMAND,         MsgCommand},
  61.     {WM_NOTIFY,          MsgNotify},
  62.     {WM_TIMER,           MsgTimer},
  63.     {WM_SIZE,            MsgSize}, 
  64.     {WM_PALETTECHANGED,  MsgPaletteChanged},
  65.     {WM_QUERYNEWPALETTE, MsgQueryNewPalette},  
  66.     {WM_CREATE,          MsgCreate},
  67.     {WM_QUERYENDSESSION, MsgQueryEndSession},   
  68.     {WM_CLOSE,           MsgClose},
  69.     {WM_DESTROY,         MsgDestroy}
  70.  
  71. };
  72.  
  73. MSDI msdiMain =
  74. {
  75.     sizeof(rgmsd) / sizeof(MSD),
  76.     rgmsd,
  77.     edwpWindow
  78. };
  79.  
  80.  
  81. // Main window command table definition.
  82. CMD rgcmd[] =
  83. {
  84.     {IDM_FILENEW,     CmdNew},            
  85.     {IDM_FILEOPEN,    CmdOpen},
  86.     {IDM_FILESAVE,    CmdSave},
  87.     {IDM_FILESAVEAS,  CmdSave},
  88.     {IDM_FILECLOSE,   CmdClose},
  89.     {IDM_FILEPRINT,   CmdPrint}, 
  90.     {IDM_FILEPAGESU,  CmdPageSetup},
  91.     {IDM_FILEPRINTSU, CmdPrintSetup},
  92.     {IDM_EXIT,        CmdExit},    
  93.        
  94.     {IDM_INFO,        CmdInfo},       
  95.     
  96.     {IDM_PIXEL,       CmdDrawMode},        
  97.     {IDM_LINE,        CmdDrawMode},
  98.     {IDM_RECT,        CmdDrawMode},
  99.     {IDM_ELLIPSE,     CmdDrawMode},
  100.     {IDM_BEZIER,      CmdDrawMode},
  101.     {IDM_FILL,        CmdFill},
  102.     {IDM_NOFILL,      CmdFill},
  103.     {IDM_CREATEPEN,   CmdCreatePen},
  104.     {IDM_CREATEBRUSH, CmdCreateBrush}, 
  105.     {IDM_CLEAR,       CmdClear},
  106.            
  107.     {IDM_ABOUT,       CmdAbout}
  108. };
  109.  
  110. CMDI cmdiMain =
  111. {
  112.     sizeof(rgcmd) / sizeof(CMD),
  113.     rgcmd,
  114.     edwpWindow
  115. };
  116.  
  117. //
  118. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  119. //
  120. //  PURPOSE:  Processes messages for the main window.
  121. //
  122. //  PARAMETERS:
  123. //    hwnd     - window handle
  124. //    uMessage - message number
  125. //    wparam   - additional information (dependant on message number)
  126. //    lparam   - additional information (dependant on message number)
  127. //
  128. //  RETURN VALUE:
  129. //    The return value depends on the message number.  If the message
  130. //    is implemented in the message dispatch table, the return value is
  131. //    the value returned by the message handling function.  Otherwise,
  132. //    the return value is the value returned by the default window procedure.
  133. //
  134. //  COMMENTS:
  135. //    Call the DispMessage() function with the main window's message dispatch
  136. //    information (msdiMain) and the message specific information.
  137. //
  138.  
  139. LRESULT CALLBACK WndProc(HWND   hwnd, 
  140.                          UINT   uMessage, 
  141.                          WPARAM wparam, 
  142.                          LPARAM lparam)
  143. {
  144.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  145. }
  146.  
  147.  
  148. //
  149. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  150. //
  151. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  152. //
  153. //  PARAMETERS:
  154. //    hwnd     - window handle
  155. //    uMessage - WM_COMMAND (Unused)
  156. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  157. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  158. //
  159. //  RETURN VALUE:
  160. //    The return value depends on the message number.  If the message
  161. //    is implemented in the message dispatch table, the return value is
  162. //    the value returned by the message handling function.  Otherwise,
  163. //    the return value is the value returned by the default window procedure.
  164. //
  165. //  COMMENTS:
  166. //    Call the DispCommand() function with the main window's command dispatch
  167. //    information (cmdiMain) and the command specific information.
  168. //
  169.  
  170. #pragma argsused
  171. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  172. {
  173.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  174. }
  175.  
  176.  
  177. //
  178. //  FUNCTION: MsgCreate(HWND, UINT, WPARAM, LPARAM)
  179. //
  180. //  PURPOSE: Handle the WM_CREATE messages for the main window.
  181. //           and call InitCommonControls() API to initialize the
  182. //           common control library. 
  183. //
  184. //  PARAMETERS:
  185. //
  186. //    hwnd      - Window handle
  187. //
  188. //  RETURN VALUE:
  189. //    Return TRUE if the StatusBar and ToolBar Windows could be created
  190. //    successfully. 
  191. //
  192. //  COMMENTS:
  193. //    Call the CreateTSBars function with the main window's window handle
  194. //    information (msdiMain).
  195. //
  196.  
  197. #pragma argsused
  198. LRESULT MsgCreate(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  199. {
  200.     int nRet = -1;                              // Assume failure
  201.     
  202.     // initialize global variable for application menu        
  203.     hMenu = GetMenu(hwnd);
  204.  
  205.     InitCommonControls();   // Initialize the common control library.
  206.  
  207.     if(CreateTBar(hwnd) && CreateSBar(hwnd))    // 1st create tool/status bars
  208.     {
  209.         hWndClient = CreateClientWindow(hwnd);  // then create client window
  210.         if (hWndClient != NULL)
  211.             nRet = 0;                           // Indicate success
  212.     }
  213.  
  214.     // Initialize some menu and toolbar options...   
  215.     EnableMenuItem(hMenu, IDM_FILESAVE, MF_DISABLED);
  216.      SendMessage(hWndToolbar, TB_ENABLEBUTTON, IDM_FILESAVE, MAKELONG(FALSE, 0));
  217.     EnableMenuItem(hMenu, IDM_FILESAVEAS, MF_DISABLED);    
  218.     EnableMenuItem(hMenu, IDM_FILECLOSE, MF_DISABLED); 
  219.     EnableMenuItem(hMenu, IDM_CLEAR, MF_DISABLED);
  220.      
  221.     CmdDrawMode(hwnd, IDM_LINE, 0, NULL);   // Set initial draw mode to Line    
  222.     CmdFill(hwnd, IDM_NOFILL, 0, NULL);     // Turn fill mode OFF
  223.     CmdFill(hwnd, IDM_FILL, 0, NULL);       // Toggle fill state (ON)
  224.  
  225.     // check palette capabilities
  226.     bPalDevice = IsPaletteDevice();
  227.     if (bPalDevice)
  228.      {
  229.         HDC hdc;
  230.  
  231.         hdc = GetDC(hwnd);
  232.  
  233.         // create a default palette for choosing pen and brush colors
  234.         // using the new halftone palette
  235.         hPalette = CreateHalftonePalette(hdc);
  236.         
  237.         ReleaseDC(hwnd, hdc);
  238.     }
  239.     
  240.      return nRet;
  241. }
  242.  
  243.  
  244. //
  245. //  FUNCTION: MsgSize(HWND, UINT, WPARAM, LPARAM)
  246. //
  247. //  PURPOSE:  This function resizes the toolbar and statusbar controls
  248. //    and the Client window which covers the rest of the main window
  249. //    client area.
  250. //
  251. //
  252. //  PARAMETERS:
  253. //
  254. //    hwnd      - Window handle  (Used)
  255. //    uMessage  - Message number (Used)
  256. //    wparam    - Extra data     (Used)
  257. //    lparam    - Extra data     (Used)
  258. //
  259. //  RETURN VALUE:
  260. //
  261. //    Always returns 0 - Message handled
  262. //
  263. //  COMMENTS:
  264. //
  265. //    When the window procdure that has the status and tool bar controls
  266. //    receives the WM_SIZE message, it has to pass the message on to these 
  267. //    controls so that these controls can adjust their size accordingly. 
  268. //   
  269. //
  270.  
  271. LRESULT MsgSize(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam) 
  272. {   
  273.     SendMessage(hWndToolbar, uMessage, wparam, lparam);
  274.     SendMessage(hWndStatusbar, uMessage, wparam, lparam);    
  275.     InitializeStatusBar(hwnd);
  276.  
  277.     // Re-size client window relative to the tool/status bars
  278.     if (wparam != SIZE_MINIMIZED)
  279.         SizeClientWindow(hwnd);
  280.  
  281.     return 0;
  282. }
  283.  
  284.  
  285. //
  286. //  FUNCTION: MsgPaletteChanged(HWND, UINT, WPARAM, LPARAM)
  287. //
  288. //  PURPOSE: Passes WM_PALETTECHANGED message to handler.
  289. //
  290. //  PARAMETERS:
  291. //    hwnd -     Handle of application window.
  292. //    uMessage - (unused)
  293. //    wparam -   Handle of window that caused palette to change
  294. //    lparam -   (unused)
  295. //
  296. //  RETURN VALUE:
  297. //    Returns the value returned by ProcessPaletteChanged.
  298. //
  299. //  COMMENTS:
  300. //
  301.  
  302. #pragma argsused
  303. LRESULT MsgPaletteChanged(HWND hwnd, UINT uMessage, WPARAM wparam,
  304.      LPARAM lparam)
  305. {
  306.     return ProcessPaletteChanged(hwnd, wparam); 
  307. }    
  308.  
  309.  
  310. //
  311. //  FUNCTION: MsgQueryNewPalette(HWND, UINT, WPARAM, LPARAM)
  312. //
  313. //  PURPOSE: Passes WM_QUERYNEWPALETTE message to handler.
  314. //
  315. //  PARAMETERS:
  316. //    hwnd     - Handle of application window.
  317. //    uMessage - (unused)
  318. //    wparam   - (unused)
  319. //    lparam   - (unused)
  320. //
  321. //  RETURN VALUE:
  322. //    Returns the value returned by ProcessQueryNewPalette.
  323. //
  324. //  COMMENTS:
  325. //
  326.  
  327. #pragma argsused
  328. LRESULT MsgQueryNewPalette(HWND hwnd, UINT uMessage, WPARAM wparam,
  329.      LPARAM lparam)
  330. {
  331.     return ProcessQueryNewPalette(hwnd); 
  332. }    
  333.  
  334.  
  335. //
  336. //  FUNCTION: MsgClose(HWND, UINT, WPARAM, LPARAM)
  337. //
  338. //  PURPOSE: Close the editor.
  339. //
  340. //  PARAMETERS:
  341. //    hwnd     - Handle of application window.
  342. //    uMessage - The message number. (unused).
  343. //    wparam   - Message specific data (unused).
  344. //    lparam   - Message specific data (unused).
  345. //
  346. //  RETURN VALUE:
  347. //    Always returns 0 - message handled.
  348. //
  349. //  COMMENTS:
  350. //
  351. //
  352.  
  353. LRESULT MsgClose(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  354. {
  355.     int nResult;
  356.     
  357.     // prompt user if there is an existing bitmap with changes
  358.     nResult = QuerySaveChanges(hwnd);
  359.     
  360.     if (nResult == IDCANCEL)
  361.         // user wants to cancel close
  362.         return 0; 
  363.     else           
  364.         // continue processing WM_CLOSE
  365.         return DefWindowProc(hwnd, uMessage, wparam, lparam);  
  366. }
  367.  
  368.     
  369. //
  370. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  371. //
  372. //  PURPOSE: Calls PostQuitMessage() after cleaning up.
  373. //
  374. //  PARAMETERS:
  375. //
  376. //    hwnd      - Window handle  (Unused)
  377. //    uMessage  - Message number (Unused)
  378. //    wparam    - Extra data     (Unused)
  379. //    lparam    - Extra data     (Unused)
  380. //
  381. //  RETURN VALUE:
  382. //
  383. //    Always returns 0 - Message handled
  384. //
  385. //  COMMENTS:
  386. //
  387. //
  388.  
  389. #pragma argsused
  390. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  391. {   
  392.     // clean up GDI objects
  393.      if (hPalette)
  394.         DeleteObject(hPalette);    
  395.     if (hBitmap)
  396.         DeleteObject(hBitmap);
  397.     if (hDIBInfo)
  398.           GlobalFree(hDIBInfo);
  399.         
  400.     PostQuitMessage(0);
  401.     return 0;
  402. }
  403.  
  404. //
  405. //  FUNCTION: MsgQueryEndSession(HWND, UINT, WPARAM, LPARAM)
  406. //
  407. //  PURPOSE: Calls QuerySaveChanges() in response to WM_QUERYENDSESSION.
  408. //
  409. //  PARAMETERS:
  410. //
  411. //    hwnd      - Window handle 
  412. //    uMessage  - Message number (Unused)
  413. //    wparam    - Extra data     (Unused)
  414. //    lparam    - Extra data     (Unused)
  415. //
  416. //  RETURN VALUE:
  417. //    TRUE  - continue session shutdown
  418. //    FALSE - user wants to abort session shutdown    
  419. //
  420. //  COMMENTS:
  421. //
  422. //
  423.  
  424. #pragma argsused
  425. LRESULT MsgQueryEndSession(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  426. {   
  427.     int nResult;
  428.     
  429.     // prompt user if there is an existing bitmap with changes
  430.     nResult = QuerySaveChanges(hwnd);
  431.     
  432.     if (nResult == IDCANCEL)
  433.           // user wants to cancel session shutdown
  434.         return FALSE; 
  435.         
  436.     // continue session shutdown
  437.     return TRUE;        
  438. }
  439.  
  440.  
  441. //
  442. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  443. //
  444. //  PURPOSE: Exit the application.
  445. //
  446. //  PARAMETERS:
  447. //    hwnd     - The window.
  448. //    wCommand - IDM_EXIT (unused)
  449. //    wNotify  - Notification number (unused)
  450. //    hwndCtrl - NULL (unused)
  451. //
  452. //  RETURN VALUE:
  453. //    Always returns 0 - command handled.
  454. //
  455. //  COMMENTS:
  456. //
  457. //
  458.  
  459. #pragma argsused
  460. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  461. {  
  462.     int nResult;
  463.      
  464.     // prompt user if there is an existing bitmap with changes
  465.     nResult = QuerySaveChanges(hwnd);
  466.     
  467.      if (nResult == IDCANCEL)
  468.         // user wants to cancel exit
  469.         return 0; 
  470.    
  471.     DestroyWindow(hwnd);
  472.     return 0;
  473. }
  474.